//Komissarov Denis

package MAINWIN;

import javax.swing.JDialog;
import Resources.CEnvironment;
import javax.swing.JOptionPane;

import API.CFile;
import API.CTask;
import CalculationCore.CCPUBlackBox;
import CalculationCore.CProgramListing;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.JList;
import javax.swing.ListSelectionModel;
import javax.swing.JCheckBox;

public class CNewTaskForm extends JDialog {

	private static final long serialVersionUID = 5159382355438056159L;
	private CTask task;
	private ArrayList<CCPUBlackBox> sProg = new ArrayList<CCPUBlackBox>();
	private ArrayList<CCPUBlackBox> fProg = new ArrayList<CCPUBlackBox>();
	final DefaultListModel listModel = new DefaultListModel();
	JList list = new JList(listModel);
	JCheckBox chkTest = new JCheckBox(CEnvironment.NewTaskSolve);
	CTaskForm pTaskForm;

	public CNewTaskForm(String title, final CCPUBlackBox calc, final CTask task) {

		this.task = task;
		setSize(357, 253);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		setResizable(isMinimumSizeSet());
		setTitle(title);
		getContentPane().setLayout(null);

		CMainForm.pTaskForm.setTask(task);
		
		JButton btnSave = new JButton(CEnvironment.NewTaskSave);
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				saveTask();
			}
		});
		btnSave.setBounds(193, 159, 141, 23);
		btnSave.setMargin(new Insets(2, 2, 2, 2));
		getContentPane().add(btnSave);

		JButton btnText = new JButton(CEnvironment.NewTaskText);
		btnText.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				pTaskForm = new CTaskForm(CEnvironment.TaskTitle, task);
				pTaskForm.setModal(true);
				pTaskForm.setText();
				pTaskForm.setVisible(true);
				task.setTask(pTaskForm.getText());
			}
		});
		btnText.setBounds(193, 130, 141, 23);
		getContentPane().add(btnText);

		JPanel panel = new JPanel();
		panel.setBounds(10, 11, 173, 201);
		getContentPane().add(panel);
		panel.setLayout(new BorderLayout(0, 0));
		panel.add(new JScrollPane(list), BorderLayout.CENTER);
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		JButton btnAdd = new JButton(CEnvironment.NewTaskAdd);
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				addTest();
			}
		});
		btnAdd.setBounds(193, 11, 141, 23);
		btnAdd.setMargin(new Insets(2, 2, 2, 2));
		getContentPane().add(btnAdd);

		JButton btnEdit = new JButton(CEnvironment.NewTaskEdit);
		btnEdit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				editTest();
			}
		});
		btnEdit.setBounds(193, 40, 141, 23);
		btnEdit.setMargin(new Insets(2, 2, 2, 2));
		getContentPane().add(btnEdit);

		JButton btnRemove = new JButton(CEnvironment.NewTaskRemove);
		btnRemove.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				delTest();
			}
		});
		btnRemove.setBounds(193, 68, 141, 23);
		btnRemove.setMargin(new Insets(2, 2, 2, 2));
		getContentPane().add(btnRemove);

		JButton btnHelp = new JButton(CEnvironment.NewTaskHelp);
		btnHelp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JOptionPane.showMessageDialog(getParent(),
						CEnvironment.NewTaskHelpText);
			}
		});
		btnHelp.setBounds(193, 188, 141, 23);
		btnHelp.setMargin(new Insets(2, 2, 2, 2));
		getContentPane().add(btnHelp);
		
		chkTest.setBounds(193, 98, 141, 23);
		getContentPane().add(chkTest);

		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent arg0) {
				if (JOptionPane.showConfirmDialog(getParent(),
						CEnvironment.NewTaskClose,
						CEnvironment.NewTaskCloseTitle,
						JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
					CMainForm.task = null;
					CMainForm.pNewTaskForm = null;
					CMainForm.taskExist();
					dispose();
				}
			}
		});

	}

	protected void saveTask() {

		String[] result = new String[sProg.size()];
		
		try {

			for (int index = 0; index < sProg.size(); index++) {

				double[] mem = sProg.get(index).getB3_34memory()
						.getDoubleArray();
				double[] stack = sProg.get(index).getB3_34stack()
						.getDoubleArray();
				int[] rstack = sProg.get(index).getB3_34returnstack()
						.getIntArray();

				result[index] = "";
				for (int i = 0; i < mem.length; i++) {
					if (i > 0 && i < mem.length)
						result[index] += ',';
					result[index] += String.valueOf(mem[i]);
				}

				result[index] += ';';

				for (int i = 0; i < stack.length; i++) {
					if (i > 0 && i < stack.length)
						result[index] += ',';
					result[index] += String.valueOf(stack[i]);
				}

				result[index] += ';';

				for (int i = 0; i < rstack.length; i++) {
					if (i > 0 && i < rstack.length)
						result[index] += ',';
					result[index] += String.valueOf(rstack[i]);
				}

				result[index] += ';';

			}

			for (int index = 0; index < sProg.size(); index++) {

				double[] mem = sProg.get(index).getB3_34memory()
						.getDoubleArray();
				double[] stack = sProg.get(index).getB3_34stack()
						.getDoubleArray();
				int[] rstack = sProg.get(index).getB3_34returnstack()
						.getIntArray();

				result[index] = "";
				for (int i = 0; i < mem.length; i++) {
					if (i > 0 && i < mem.length)
						result[index] += ',';
					result[index] += String.valueOf(mem[i]);
				}

				result[index] += ';';

				for (int i = 0; i < stack.length; i++) {
					if (i > 0 && i < stack.length)
						result[index] += ',';
					result[index] += String.valueOf(stack[i]);
				}

				result[index] += ';';

				for (int i = 0; i < rstack.length; i++) {
					if (i > 0 && i < rstack.length)
						result[index] += ',';
					result[index] += String.valueOf(rstack[i]);
				}

				result[index] += ";|";

				mem = fProg.get(index).getB3_34memory()
						.getDoubleArray();
				stack = fProg.get(index).getB3_34stack()
						.getDoubleArray();
				rstack = fProg.get(index).getB3_34returnstack()
						.getIntArray();

				for (int i = 0; i < mem.length; i++) {
					if (i > 0 && i < mem.length)
						result[index] += ',';
					result[index] += String.valueOf(mem[i]);
				}

				result[index] += ';';

				for (int i = 0; i < stack.length; i++) {
					if (i > 0 && i < stack.length)
						result[index] += ',';
					result[index] += String.valueOf(stack[i]);
				}

				result[index] += ';';

				for (int i = 0; i < rstack.length; i++) {
					if (i > 0 && i < rstack.length)
						result[index] += ',';
					result[index] += String.valueOf(rstack[i]);
				}

				result[index] += ";";
				
			}
			
			task.setTests(result);
			
		} catch (Exception ex) {
			System.out.println("CNewTaskForm: saveTask: convert error");
		}

		CFile file = new CFile(getParent());
		file.save(task.getTask(), task.getTests());

	}

	// Adding new test
	protected void addTest() {

		// Preset test

		CCPUBlackBox tmpCalc0 = new CCPUBlackBox(true, new CProgramListing(
				CEnvironment.RealProgramListingSize, true));
		CRegistreForm reg0 = new CRegistreForm(CEnvironment.RegistreTitle1,
				tmpCalc0);

		reg0.setModal(true);
		reg0.setEditable(1);
		reg0.setVisible(true);
		reg0.dispose();
		sProg.add(tmpCalc0);

		
		// Solve test and result test

		CCPUBlackBox tmpCalc1 = new CCPUBlackBox(true, new CProgramListing(
				CEnvironment.RealProgramListingSize, true));
		CRegistreForm reg1 = new CRegistreForm(CEnvironment.RegistreTitle2,
				tmpCalc1);

		if (chkTest.isSelected()) {
			tmpCalc0.setCurrentProgram(CMainForm.calculator.getCurrentProgram());
			CTask tsk = new CTask("", null, tmpCalc0);
			tsk.run();
			CMainForm.LEDout(" 0.");
			tmpCalc1.setB3_34memory(tmpCalc0.getB3_34memory());
			tmpCalc1.setB3_34stack(tmpCalc0.getB3_34stack());
			tmpCalc1.setB3_34returnstack(tmpCalc0.getB3_34returnstack());
		}
		
		reg1.setModal(true);
		reg1.setUncheck(true);
		reg1.setEditable(2);
		reg1.registreUpdate();
		reg1.setVisible(true);
		reg1.dispose();
		fProg.add(tmpCalc1);

		listModel.addElement(CEnvironment.NewTaskTable
				+ Integer.valueOf(listModel.getSize() + 1));

	}

	// Edit task
	protected void editTest() {

		if (list.getSelectedIndex() < 0)
			return;

		// Preset test

		CCPUBlackBox tmpCalc0 = sProg.get(list.getSelectedIndex());
		CRegistreForm reg0 = new CRegistreForm(CEnvironment.RegistreTitle1,
				tmpCalc0);

		reg0.setModal(true);
		reg0.setEditable(1);
		reg0.setVisible(true);
		reg0.dispose();

		sProg.set(list.getSelectedIndex(), tmpCalc0);

		// Result test

		CCPUBlackBox tmpCalc1 = fProg.get(list.getSelectedIndex());
		CRegistreForm reg1 = new CRegistreForm(CEnvironment.RegistreTitle2,
				tmpCalc1);

		reg1.setModal(true);
		reg1.setEditable(2);
		reg1.setVisible(true);
		reg1.dispose();

		fProg.set(list.getSelectedIndex(), tmpCalc1);

	}

	// Delete task
	protected void delTest() {

		if (list.getSelectedIndex() < 0)
			return;

		sProg.remove(list.getSelectedIndex());
		fProg.remove(list.getSelectedIndex());

		for (int i = list.getSelectedIndex() + 1; i < listModel.size(); i++)
			listModel.set(i, CEnvironment.NewTaskTable + Integer.valueOf(i));

		listModel.remove(list.getSelectedIndex());

	}
}
